<template>
  <div style="padding: 10px">
    <div style="margin: 10px 0">

      <el-button type="danger" @click="Show" v-if="!isShow">批量删除</el-button>
      <el-button type="danger" @click="deleteBatch" v-if="isShow">删除</el-button>
      <el-button type="danger" @click="Show" v-if="isShow">取消</el-button>
      <el-upload   action="http://localhost:9090/book/import"   :on-success="handleUploadSuccess"   :show-file-list=false   :limit="1"   accept='.xlsx'   style="display: inline-block; margin: 0 10px" >
        <el-button type="primary">导入</el-button>
      </el-upload>
      <el-button type="primary" @click="exportUser">导出</el-button>
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%;margin-left: 500px" clearable/>
      <el-button type="primary" style="margin-left: 10px" @click="load">查询</el-button>
    </div>
    <el-table

        :data="tableData"
        border
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
    >
      <el-table-column v-if="isShow" type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80" sortable></el-table-column>
      <el-table-column prop="no" label="订单编号"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="time" label="订单时间"></el-table-column>
      <el-table-column prop="state" label="支付状态"></el-table-column>
      <el-table-column prop="total" label="订单总价"></el-table-column>
      <el-table-column prop="paymentTime" label="支付时间"></el-table-column>
      <el-table-column prop="alipayNo" label="支付宝宝流水号"></el-table-column>
      <el-table-column prop="returnTime" label="退款时间"></el-table-column>
      <el-table-column label="支付"  width="180" align="center">
        <template v-slot="scope">
          <el-button type="primary" @click="pay(scope.row)" :disabled="scope.row.state !== '待支付'">支 付</el-button>
          <el-button type="danger" @click="returnPay(scope.row)" :disabled="scope.row.state !== '已支付'">退款</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="240">
        <template #default="scope">
          <el-button type="primary" @click="pay(scope.row)">支 付
          </el-button>
          <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'Order',
  components: {

  },
  data() {
    return {
      isShow:false,
      user: {},
      loading: true,
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      ids:[],
    }
  },
  created() {
    let userStr= sessionStorage.getItem("user") || "{}"
    this.user= JSON.parse(userStr)


    request.get("/api/user/"+this.user.id).then( res =>{
          if (res.code === '0'){
            this.user=res.data
          }
        }
    )
    this.load();
  },
  methods: {
    //wxpfjh9904@sandbox.com
    handleSelectionChange(val){
      console.log(this.ids)
      this.ids=val.map(v => v.id)
    },
    Show(){
      this.isShow=!this.isShow
    },
    deleteBatch(){
      if(!this.ids.length){
        this.$message.warning("请选择数据!")
        return
      }
      request.post("/api/orders/deleteBatch",this.ids).then(res=>{
        if(res.code === '0'){
          this.$message.success("批量删除成功")
          this.load()
        }else{
          this.$message.error(res.msg)
        }
      })
    },
    pay(row){
      window.open(`http://localhost:9090/alipay/pay?subject=${row.name}&traceNo=${row.no}&totalAmount=${row.total}`);
    },
    returnPay(row) {
      console.log(row)
      const url = `http://localhost:9090/alipay/return?totalAmount=${row.total}&alipayTraceNo=${row.alipayNo}&traceNo=${row.no}`
      request.get(url).then(res => {
        if(res.code === '0') {
          this.$message.success("退款成功")
        }  else {
          this.$message.error(res.msg)
        }
        this.load()
      })
    },
    load() {
      this.loading = true
      request.get("/api/orders", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        this.loading = false
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
      if (this.$refs['upload']) {
        this.$refs['upload'].clearFiles()  // 清除历史文件列表
      }
    },
    save() {
      if (this.form.id) {  // 更新
        request.put("/api/orders", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }  else {  // 新增
        request.post("/api/orders", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }

          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.$nextTick(() => {
        if (this.$refs['upload']) {
          this.$refs['upload'].clearFiles()  // 清除历史文件列表
        }
      })

    },
    handleDelete(id) {
      console.log(id)
      request.delete("/api/orders/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()  // 删除之后重新加载表格的数据
      })
    },
    handleSizeChange(pageSize) {   // 改变当前每页的个数触发
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {  // 改变当前页码触发
      this.currentPage = pageNum
      this.load()
    }
  }
}
</script>
